<?php

$adir = "jsmin";			//directory to store output, must not be one of the source directories
require_once('jsmin.php');		//load javascript minifier	
require_once('cssmin-v3.0.1.php');		//load css minifier	



//delete older files in library
//$unix_timestamp.='';		//make a string
if (file_exists($adir) && $dh = opendir($adir)) 
	{
	while($fn = readdir($dh)) if ($fn[0] != ".")
		{
		if (strpos($fn,$unix_timestamp)===false)
			{
			unlink($adir.'/'.$fn);
			//echo $fn,' deleted<br>';
			}
		}	
	closedir($dh);
	}
else
	echo 'failed to open ',$adir,' ',date("r"),PHP_EOL;
	
file_compress('min.js',array(
						'js/bootstrap.js',
						'js/jquery.alertify.min.js',
						'js/chosen.jquery.min.js',
						'js/jquery.validate.js',
						'js/modernizr.custom.js',
						'js/jquery.ba-bbq.min.js',
						'js/jquery.form.js',
						'js/bootstrap-modal.js',
						'js/bootstrap-modalmanager.js',
						'kendoui/js/kendo.all.min.js',
					));	

function file_compress($file_name,$file_input) {
	global $adir;
	$pos=strrpos($file_name,'.');				//get last . in file name
	if ($pos==false)
		die ('illogical response from strrpos');
	$fn=substr($file_name,0,$pos).substr($file_name,$pos);	//put timestamp into file name

	$fl=null;						//clear file data variable
	foreach($file_input as $value)				//merge files in the group
		$fl.= file_get_contents($value).' ';
	
	if (strtolower(substr($file_name,$pos+1,2)) == 'js')	
		$fl = JSMin::minify($fl);		
	else
		$fl = CssMin::minify($fl);
		
 	file_put_contents ($adir.'/'.$fn,$fl);

}
?>